package com.ht.pool;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

public class LocalSourcePool {
	
	public static volatile DruidDataSource druidDataSource =null;
	
	private LocalSourcePool(){
	}
	
	/*static{
		Properties properties = new Properties();
		InputStream is = LocalSourcePool.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			properties.load(is);
			druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
			if(is!=null){
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	public static DruidDataSource getDruidDataSource(){

		if(druidDataSource==null){
			synchronized (LocalSourcePool.class) {
				if(druidDataSource==null){
					Properties properties = new Properties();
					InputStream is = LocalSourcePool.class.getClassLoader().getResourceAsStream("application.properties");
					try {
						properties.load(is);
						druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
						if(is!=null){
							is.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					} catch (Exception e) {
						e.printStackTrace();
					} finally{
						if(is!=null){
							try {
								is.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		}
		return druidDataSource;
	}
	
	public static DruidPooledConnection getConnection() throws SQLException{
		
		return getDruidDataSource().getConnection();
		
	}
	
	
	
	
}
